/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */


package ru.teleology.cubefs.api.reflection;


@NativeType(name = Types.NUMBER_TYPE)
public class Number extends Typed {

	public Number(long handle) {
		super(handle);
	}
	
	public int getIntValue() {
		return getIntValue(getHandle());
	}
	
	public long   getLongValue() {
		return getLongValue(getHandle());
	}
	
	public float  getFloatValue() {
		return getFloatValue(getHandle());
	}
	
	public double getDoubleValue() {
		return getDoubleValue(getHandle());
	}
	
	public short  getShortValue() {
		return getShortValue(getHandle());
	}
	
	public char   getCharacterValue() {
		return getCharacterValue(getHandle());
	}
	
	public byte   getByteValue() {
		return getByteValue(getHandle());
	}
	
	public boolean   getBooleanValue() {
		return getBooleanValue(getHandle());
	}
	
	public void 	setIntValue(int value) {
		setIntValue(getHandle(), value);
	}
	
	public void   	setLongValue(long value) {
		setLongValue(getHandle(), value);
	}
	
	public void  	setFloatValue(float value) {
		setFloatValue(getHandle(), value);
	}
	
	public void 	setDoubleValue(double value) {
		setDoubleValue(getHandle(), value);
	}
	
	public void  	setShortValue(short value) {
		setShortValue(getHandle(), value);
	}
	
	public void   	setCharacterValue(char value) {
		setCharacterValue(getHandle(), value);
	}
	
	public void   	setByteValue(byte value) {
		setByteValue(getHandle(), value);
	}
	
	public void   	setBooleanValue(boolean value) {
		setBooleanValue(getHandle(), value);
	}
	
	public Object toObject() {
		Type clazz = type();
		
		if (clazz == Types.JInt) {
			return new Integer(getIntValue());
		}
		else if (clazz == Types.JLong) {
			return new Long(getLongValue());
		}
		else if (clazz == Types.JBoolean) {
			return new Boolean(getBooleanValue());
		}
		else if (clazz == Types.JChar) {
			return new Character(getCharacterValue());
		}
		else if (clazz == Types.JShort) {
			return new Short(getShortValue());
		}
		else if (clazz == Types.JByte) {
			return new Byte(getByteValue());
		}
		else if (clazz == Types.JDouble) {
			return new Double(getDoubleValue());
		}
		else if (clazz == Types.JFloat) {
			return new Float(getFloatValue());
		}
		else {
			throw new RuntimeException("Unsupported "+clazz);
		}
	}
	
	@Override
	public String toString() {
		return toObject().toString();
	}
	
	public Object toObject(Class<?> clazz) {
		if (clazz == Integer.class) {
			return new Integer(getIntValue());
		}
		else if (clazz == Long.class) {
			return new Long(getLongValue());
		}
		else if (clazz == Boolean.class) {
			return new Boolean(getBooleanValue());
		}
		else if (clazz == Character.class) {
			return new Character(getCharacterValue());
		}
		else if (clazz == Short.class) {
			return new Short(getShortValue());
		}
		else if (clazz == Byte.class) {
			return new Byte(getByteValue());
		}
		else if (clazz == Double.class) {
			return new Double(getDoubleValue());
		}
		else if (clazz == Float.class) {
			return new Float(getFloatValue());
		}
		else {
			throw new RuntimeException("Unsupported "+clazz);
		}
	}
		
	private static native int    getIntValue(long me);
	private static native long   getLongValue(long me);
	private static native float  getFloatValue(long me);
	private static native double getDoubleValue(long me);
	private static native short  getShortValue(long me);
	private static native char   getCharacterValue(long me);
	private static native byte   getByteValue(long me);
	private static native boolean getBooleanValue(long me);
	
	private static native void 	setIntValue(long me, int value);
	private static native void   setLongValue(long me, long value);
	private static native void  	setFloatValue(long me, float value);
	private static native void 	setDoubleValue(long me, double value);
	private static native void  	setShortValue(long me, short value);
	private static native void   setCharacterValue(long me, char value);
	private static native void   setByteValue(long me, byte value);
	private static native void   setBooleanValue(long me, boolean value);
}
